package com.wang.nowcoder.controller;

import com.wang.nowcoder.pojo.User;
import com.wang.nowcoder.service.LikeService;
import com.wang.nowcoder.util.CommunityUtil;
import com.wang.nowcoder.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang
 * @ClassName LikeController
 * @Description TODO
 * @date 2021/12/27 16:43
 * @Version 1.0
 */
@Controller
public class LikeController {

    @Autowired
    private LikeService likeService;
    @Autowired
    private HostHolder hostHolder;

    @PostMapping("/like")
    @ResponseBody
    public String like(int pojoType,int pojoId,int pojoUserId){
        User user = hostHolder.getUser();
        //实现点赞
        likeService.like(user.getId(),pojoType,pojoId,pojoUserId);
        //获取数量
        long likeCount = likeService.selectPojoLikeCount(pojoType, pojoId);
        //状态
        int likeStatus = likeService.selectPojoLikeStatus(user.getId(),pojoType,pojoId);
        Map<String,Object> map = new HashMap<>();
        map.put("likeCount",likeCount);
        map.put("likeStatus",likeStatus);

        return CommunityUtil.getJSONString(0,null,map);
    }
}
